import * as NSBase from '../NetServiceBase';
import { IAuthManager } from '../IManager';
import { IsNull } from '../../TYLibrary/Common/GlobalExtend';
import { ManagerBase } from './ManagerBase';
import { WXAuthType } from '../EntityBase';
import { WXScopeType } from '../../wxAPI_';

//NSBase.TYLogins

export class AuthManager extends ManagerBase implements IAuthManager {
    public TYLoginUrl: string;

	private async _CheckAuthSetting() {
		let authSetting = await this.m_App.Wx_GetSetting();
		if (authSetting[WXScopeType.userInfo] == true) {
			this.m_App.WXLoginInfo = await NSBase.TYLogins(this.TYLoginUrl);
			this.m_App.WXLoginInfo.AuthSetting = WXAuthType.AuthSuccess;
            wx.setStorageSync('LoginPICUrl', this.m_App.WXLoginInfo.PICUrl);
            wx.setStorageSync('LoginName', this.m_App.WXLoginInfo.Name);
			await this.m_App.ModuleManager.MemberManager.Init();
		} 
		else if (IsNull(authSetting[WXScopeType.userInfo]))
			this.m_App.WXLoginInfo.AuthSetting = WXAuthType.NoAuth;
		else
			this.m_App.WXLoginInfo.AuthSetting = WXAuthType.AuthReject;
	}

	public async Init() {
		await this._CheckAuthSetting();
	}

	public async Refresh() {
		await this._CheckAuthSetting();
	}
}
